#!/bin/bash
# Installation:
#  Copy to /etc/bash_completion.d/modman
# or
#  Append to ~/.bash_completion
#
_modman()
{
	local cur prev opts mm module
	COMPREPLY=()
	cur="${COMP_WORDS[COMP_CWORD]}"
	prev="${COMP_WORDS[COMP_CWORD-1]}"
	mm=$(_modman_get_mm)
	module=$(_modman_get_module)
	case "${prev}" in
		modman)
			opts="init --help --version --tutorial"
			if [ -n "$mm" ]; then
				opts="${opts} list update-all deploy-all repair clean"
				opts="${opts} checkout clone deploy update"
			fi
			;;
		init | --help | --version | --tutorial | list | repair | --force | --copy)
			opts=""
			;;
		update-all | deploy-all | checkout | clone)
			opts="--force"
			;;
		deploy | update)
			opts=$(modman list)
			;;
		*)
			[ -d "$mm/${prev}" ] && opts="--force"
			;;
	esac
	COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
	return 0
}
 
_modman_get_mm()
{
	local root _pwd
	_pwd=$(pwd)
	root=$(pwd -P)
	while ! [ -d $root/.modman ]; do
		if [ "$root" = "/" ]; then
			cd $_pwd && return 1
		fi
		cd ..
		root=`pwd`
	done
	cd $_pwd
	echo "$root/.modman"
}
 
_modman_get_module()
{
	local root module modpath mm _pwd
	_pwd=`pwd`
	mm=$(_modman_get_mm)
	[ -z "$mm" ] && return 1
	root="$(dirname $mm)"
	module=''
	while [ "$root" != "$(pwd)" ]; do
		modpath=`pwd`
		if [ "$mm" = "$(dirname $modpath)" ]; then
			module=$(basename $modpath)
			break
		fi
		cd ..
	done
	cd $_pwd
	[ -z "$module" ] && return 1
	echo $module
}
 
complete -F _modman modman
